package com.ljkj.cordial.chat.presenter.response;

import android.app.Activity;

import com.lanjiu.lib.imclient.ims.IMSContacts;
import com.lanjiu.lib.imclient.ims.IMSUtils;
import com.lanjiu.pro.business.BusinessProtocolEntities;
import com.lanjiu.pro.business.BusinessProtocolMessages;
import com.ljkj.cordial.chat.dao.notify.GroupMemberTable;
import com.ljkj.cordial.chat.dao.notify.GroupTable;

import java.util.ArrayList;
import java.util.List;

public class GroupInfoProtocolResponse extends AResponseBusinessPresenter {
    @Override
    public void onResponse(Activity activity, boolean isOnScreen, String msgType, Object msg) {
        BusinessProtocolMessages.GroupInfoProtocol response = (BusinessProtocolMessages.GroupInfoProtocol) msg;
        GroupTable groupTable = null;
        if (response.hasRegisteredGroup()) {
            groupTable = getRegisteredGroup(response.getRegisteredGroup());
            if (response.getRegisteredGroupMemberList() != null && !response.getRegisteredGroupMemberList().isEmpty()) {
                List<GroupMemberTable> groupMemberTables = new ArrayList<>();
                List<BusinessProtocolEntities.RegisteredGroupMember> list = response.getRegisteredGroupMemberList();
                for (BusinessProtocolEntities.RegisteredGroupMember member : list) {
                    if (GroupMemberTable.OWNER == member.getGroupMemberType()) {
                        groupTable.setGroupOwnerId(member.getGroupMemberUserId());
                    }
                    groupMemberTables.add(getGroupMemberTable(member.getGroupMemberId(), member.getGroupMemberName(), member.getGroupMemberNick(), member.getGroupMemberUserId(), member.getGroupMemberAlias(), member.getOnlineStatus(), IMSContacts.UserType.REGISTERED_USER, member.getGroupMemberAvatar(), member.getGroupMemberType(), groupTable.getGroupId(), groupTable.getGroupName(), groupTable.getGroupType()));
                }
                insert(groupTable, groupMemberTables);
            }
        } else if (response.hasTouristsGroup()) {
            groupTable = getTouristsGroup(response.getTouristsGroup());
            if (response.getTouristsGroupMemberList() != null && !response.getTouristsGroupMemberList().isEmpty()) {
                List<BusinessProtocolEntities.TouristsGroupMember> list = response.getTouristsGroupMemberList();
                List<GroupMemberTable> groupMemberTables = new ArrayList<>();
                for (BusinessProtocolEntities.TouristsGroupMember member : list) {
                    if (GroupMemberTable.OWNER == member.getGroupMemberType()) {
                        groupTable.setGroupOwnerId(member.getGroupMemberUserId());
                    }
                    groupMemberTables.add(getGroupMemberTable(member.getGroupMemberId(), member.getGroupMemberName(), member.getGroupMemberNick(), member.getGroupMemberUserId(), member.getGroupMemberAlias(), member.getOnlineStatus(), IMSContacts.UserType.TOURISTS_USER, member.getGroupMemberAvatar(), member.getGroupMemberType(), groupTable.getGroupId(), groupTable.getGroupName(), groupTable.getGroupType()));
                }
                insert(groupTable, groupMemberTables);
            }
        }

        if (isOnScreen) {
            refresh(activity);
        }
    }

    private GroupTable getRegisteredGroup(BusinessProtocolEntities.RegisteredGroup group) {
        GroupTable groupTable = GroupTable.queryRowByGroupIdNotNull(group.getGroupId());
        groupTable.setGroupName(group.getGroupName());
        groupTable.setGroupId(group.getGroupId());
        groupTable.setGroupType(IMSContacts.UserType.REGISTERED_USER);
        if (group.hasGroupAnnouncements()) {
            BusinessProtocolEntities.GroupAnnouncements groupS = group.getGroupAnnouncements();
            groupTable.setAnnouncementContent(groupS.getAnnouncementContent());
            groupTable.setAnnouncementTitle(groupS.getAnnouncementTitle());
            groupTable.setAnnouncementId(groupS.getAnnouncementId());
        }
        if (group.hasSettings()) {
            BusinessProtocolEntities.Settings set = group.getSettings();
            groupTable.setSnapchatStatus(set.getSnapchatStatus());
            groupTable.setSnapchatTimeLength(set.getSnapchatTimeLength());
            groupTable.setSnapchatTimeUnit(set.getSnapchatTimeUnit());
        }
        return groupTable;
    }

    private GroupTable getTouristsGroup(BusinessProtocolEntities.TouristsGroup group) {
        GroupTable groupTable = GroupTable.queryRowByGroupIdNotNull(group.getGroupId());

        groupTable.setGroupName(group.getGroupName());
        groupTable.setGroupId(group.getGroupId());
        groupTable.setGroupType(IMSContacts.UserType.TOURISTS_USER);
        groupTable.setGroupOwnerId(group.getGroupOwnerUserId());
        if (group.hasGroupAnnouncements()) {
            BusinessProtocolEntities.GroupAnnouncements groupS = group.getGroupAnnouncements();
            groupTable.setAnnouncementContent(groupS.getAnnouncementContent());
            groupTable.setAnnouncementTitle(groupS.getAnnouncementTitle());
            groupTable.setAnnouncementId(groupS.getAnnouncementId());
        }
        if (group.hasSettings()) {
            BusinessProtocolEntities.Settings set = group.getSettings();
            groupTable.setSnapchatStatus(set.getSnapchatStatus());
            groupTable.setSnapchatTimeLength(set.getSnapchatTimeLength());
            groupTable.setSnapchatTimeUnit(set.getSnapchatTimeUnit());
        }
        return groupTable;
    }

    private GroupMemberTable getGroupMemberTable(String groupMemberId, String groupMemberName, String groupMemberNick, String groupMemberUserId, String groupMemberAlias, String onlineStatus, String userType, String groupMemberAvatar, int groupMemberType, String groupId, String groupName, String groupType) {
        GroupMemberTable groupMemberTable = GroupMemberTable.create(groupId, groupMemberUserId);
        groupMemberTable.setGroupMemberId(groupMemberId);
        groupMemberTable.setGroupMemberName(groupMemberName);
        groupMemberTable.setGroupMemberNick(groupMemberNick);
        groupMemberTable.setGroupMemberUserId(groupMemberUserId);
        groupMemberTable.setGroupMemberAlias(groupMemberAlias);
        groupMemberTable.setOnlineStatus(onlineStatus);
        groupMemberTable.setUserType(IMSUtils.userTypeAnalysis(groupMemberUserId));
        groupMemberTable.setGroupMemberAvatar(groupMemberAvatar);
        groupMemberTable.setGroupMemberType(groupMemberType);
        groupMemberTable.setGroupId(groupId);
        groupMemberTable.setGroupName(groupName);
        groupMemberTable.setGroupType(groupType);
        return groupMemberTable;

    }

    @Override
    public void insert(Object... table) {
        super.insert(table);
        try {
            GroupTable groupTable = (GroupTable) table[0];
            List<GroupMemberTable> groupMemberTables = (List<GroupMemberTable>) table[1];
            GroupTable.insert(groupTable);
            if (!groupMemberTables.isEmpty()) {
                GroupMemberTable.insertOrUpdate(groupMemberTables);
            }
//            ChatContentTable cct = new ChatContentTable();
//            cct.setChatType(Canstants.GROUP_CHAT);
//            cct.setTargetId(groupTable.getGroupId());
//            cct.setTargetName(groupTable.getGroupName());
//            cct.setTargetType(groupTable.getGroupType());
//            List<String> urls = GroupMemberTable.getMemberAvatarUrls(groupTable.getGroupId());
//            cct.setTargetAvatarUrl(TextUtils.join(",", urls));
//            ChatContentTable.insert(cct);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
